from fastapi import Request
from uvicorn.loops import asyncio


class RequestQueueMiddleware:
    def __init__(self, max_queue_size=100):
        self.semaphore = asyncio.Semaphore(max_queue_size)

    async def __call__(self, request: Request, call_next):
        async with self.semaphore:
            return await call_next(request)
